# 导入cv模块
import cv2 as cv


# 构建检测函数
def face_detect_demo():
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    # 导入分类器
    face_detect = cv.CascadeClassifier('D:/Programming_Softwares/OpenCV/opencv/sources/data/haarcascades'
                                       '/haarcascade_frontalface_default.xml')
    face = face_detect.detectMultiScale(gray)
    for x, y, w, h in face:
        cv.rectangle(img, (x, y), (x + w, y + h), color=(0, 0, 255), thickness=2)
    cv.imshow('result', img)


# 获取图像
img = cv.imread('face1.jpg')
# 检测函数
face_detect_demo()
# 等待
while True:
    if ord('q') == cv.waitKey(0):
        break
# 释放内存
cv.destroyAllWindows()
